package com.bzy.bladeauthapi.vo;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class QueryWrapperVO {
	private String[] select;//设置 SELECT 查询字段
	private Map<String, Object> eq;//等于=
	private String[] orderByDesc;//DESC 排序 ORDER BY
	private String[] orderByAsc;//ASC 排序 ORDER BY
	private Map<String, Object> ne;//不等于<>
	private Map<String, Object> gt;//大于>
	private Map<String, Object> ge;//大于等于>=
	private Map<String, Object> lt;//小于<
	private Map<String, Object> le;//小于等于<=
	private Map<String, Object> like;//模糊查询 LIKE
	private Map<String, Object[]> in;//IN 查询
	private String[] isNull;//NULL 值查询
	private String[] groupBy;//分组 GROUP BY
	/*private String last;*/

	public static <T> QueryWrapper<T> getQueryWrapper(String queryWrapperVo) {
		QueryWrapperVO queryWrapperVO = JSONObject.parseObject(queryWrapperVo, QueryWrapperVO.class);
		QueryWrapper<T> queryWrapper = new QueryWrapper<>();
		if (queryWrapperVO.select != null) {
			queryWrapper.select(queryWrapperVO.select);
		}
		if (queryWrapperVO.eq != null) {
			queryWrapperVO.eq.entrySet().stream().forEach(en -> queryWrapper.eq(en.getKey(), en.getValue()));
		}
		if (queryWrapperVO.orderByDesc != null) {
			queryWrapper.orderByDesc(queryWrapperVO.orderByDesc);
		}
		if (queryWrapperVO.orderByAsc != null) {
			queryWrapper.orderByAsc(queryWrapperVO.orderByAsc);
		}
		if (queryWrapperVO.ne != null) {
			queryWrapperVO.ne.entrySet().stream().forEach(en -> queryWrapper.ne(en.getKey(), en.getValue()));
		}
		if (queryWrapperVO.gt != null) {
			queryWrapperVO.gt.entrySet().stream().forEach(en -> queryWrapper.gt(en.getKey(), en.getValue()));
		}
		if (queryWrapperVO.ge != null) {
			queryWrapperVO.ge.entrySet().stream().forEach(en -> queryWrapper.ge(en.getKey(), en.getValue()));
		}
		if (queryWrapperVO.lt != null) {
			queryWrapperVO.lt.entrySet().stream().forEach(en -> queryWrapper.lt(en.getKey(), en.getValue()));
		}
		if (queryWrapperVO.le != null) {
			queryWrapperVO.le.entrySet().stream().forEach(en -> queryWrapper.le(en.getKey(), en.getValue()));
		}
		if (queryWrapperVO.like != null) {
			queryWrapperVO.like.entrySet().stream().forEach(en -> queryWrapper.like(en.getKey(), en.getValue()));
		}
		if (queryWrapperVO.in != null) {
			queryWrapperVO.in.entrySet().stream().forEach(en -> queryWrapper.in(en.getKey(), en.getValue()));
		}
		if (queryWrapperVO.isNull != null) {
			for (String s : queryWrapperVO.isNull) {
				queryWrapper.isNull(s);
			}
		}
		if (queryWrapperVO.groupBy != null) {
			queryWrapper.groupBy(queryWrapperVO.groupBy);
		}
		/*if(StringUtils.isNotBlank(queryWrapperVO.last)){
			queryWrapper.last(queryWrapperVO.last);
		}*/
		return queryWrapper;
	}

	public QueryWrapperVO select(String... columns) {
		this.select = columns;
		return this;
	}

	public QueryWrapperVO eq(String column, Object val) {
		if (this.eq == null) {
			this.eq = new HashMap<>();
		}
		eq.put(column, val);
		return this;
	}

	public QueryWrapperVO orderByDesc(String... columns) {
		this.orderByDesc = columns;
		return this;
	}

	public QueryWrapperVO orderByAsc(String... columns) {
		this.orderByAsc = columns;
		return this;
	}

	public QueryWrapperVO ne(String column, Object val) {
		if (this.ne == null) {
			this.ne = new HashMap<>();
		}
		this.ne.put(column, val);
		return this;
	}

	public QueryWrapperVO gt(String column, Object val) {
		if (this.gt == null) {
			this.gt = new HashMap<>();
		}
		this.gt.put(column, val);
		return this;
	}

	public QueryWrapperVO ge(String column, Object val) {
		if (this.ge == null) {
			this.ge = new HashMap<>();
		}
		this.ge.put(column, val);
		return this;
	}

	public QueryWrapperVO lt(String column, Object val) {
		if (this.lt == null) {
			this.lt = new HashMap<>();
		}
		this.lt.put(column, val);
		return this;
	}

	public QueryWrapperVO le(String column, Object val) {
		if (this.le == null) {
			this.le = new HashMap<>();
		}
		this.le.put(column, val);
		return this;
	}

	public QueryWrapperVO like(String column, Object val) {
		if (this.like == null) {
			this.like = new HashMap<>();
		}
		this.like.put(column, val);
		return this;
	}

	public QueryWrapperVO in(String column, Object... values) {
		if (this.in == null) {
			this.in = new HashMap<>();
		}
		this.in.put(column, values);
		return this;
	}

	public QueryWrapperVO isNull(String... column) {
		this.isNull = column;
		return this;
	}

	public QueryWrapperVO groupBy(String... columns) {
		this.groupBy = columns;
		return this;
	}
	/*public QueryWrapperVO last(String last) {
		this.last = last;
		return this;
	}*/

	public String toJsonString() {
		return JSONObject.toJSONString(this);
	}
}
